import axios from "axios";
import Setting from "./setting";
import { getToken } from "./auth";
import router from "../router/index";
import { Message } from "element-ui";
import { download } from './down.js'

const service = axios.create({
  baseURL: Setting.apiBaseUrl,
  timeout: 5000,
});
const downloadRequest = axios.create({
  baseURL: Setting.apiBaseUrl,
  timeout: 600000, // 请求超时时间
});
const downPost = (params) => {
  return downloadRequest({
    ...params,
    method: "post",
    responseType: "blob",
  });
};
downloadRequest.interceptors.response.use((response) => {
    // console.log(response);
    const contentType = response.headers['content-type']
    // const contentDisposition = response.headers['content-disposition']
    const filename = '列表.xlsx'
    download(response.data, contentType, filename)
  
    return response.data
  })
// axios拦截器
//拦截请求
service.interceptors.request.use(
  (config) => {
    const token = getToken();
    // console.log(token);
    if (token) {
      config.headers["authorization"] = token;
      // console.log(config.headers.authorization);
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
//拦截响应
service.interceptors.response.use((response) => {
  let status = response.data ? response.data.code : 0;
  switch (status) {
    case 200:
      return response.data;
    case 401:
      router.replace({ path: "/admin/login" });
      break;
    default:
      Message({
        type: "error",
        message: response.data.message || "错误",
        showClose: true,
      });
      return Promise.reject(response.data);
  }
});
export default service;
export { downPost };
